package com.upcode.weighing.config.util;


import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.Base64;
import java.util.Scanner;

import javax.crypto.Cipher;



public class GenDatabasePwdTools {
	
	
public static void main(String[] args) throws Exception {
        
		java.util.Scanner scanner = new Scanner(System.in);
		String userName = scanner.next();
		System.out.println("your input userName:" + userName);
		String password = scanner.next();
		System.out.println("your input password:" + password);
		
		
		//String password = args[0];
        String[] arr = genKeyPair(512);
        System.out.println("privateKey:" + arr[0]);
        System.out.println("publicKey:" + arr[1]);
        String encU = encrypt(arr[0], userName);
        System.out.println("password:" + encU);
        String encP = encrypt(arr[0], password);
        System.out.println("password:" + encP);
        
       
        
       File file = new File("E:/dbinfo_test3_etc.cnf");
       FileOutputStream fileOutPutStream = new FileOutputStream(file);
       DataOutputStream dof = new DataOutputStream(fileOutPutStream);
       dof.writeUTF(arr[1]);
       dof.writeUTF(encU);
       dof.writeUTF(encP);
	}

	
	public static String[] genKeyPair(int keySize)
			throws NoSuchAlgorithmException, NoSuchProviderException {
		byte[][] keyPairBytes = genKeyPairBytes(keySize);
		String[] keyPairs = new String[2];

		keyPairs[0] = Base64.getEncoder().encodeToString(keyPairBytes[0]);
		keyPairs[1] = Base64.getEncoder().encodeToString(keyPairBytes[1]);
		
		return keyPairs;
	}
	
	
	public static String encrypt(String key, String plainText) throws Exception {
		byte[] keyBytes =Base64.getDecoder().decode(key);
		return encrypt(keyBytes, plainText);
	}
	
	public static String encrypt(byte[] keyBytes, String plainText)
			throws Exception {
		PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory factory = KeyFactory.getInstance("RSA", "SunRsaSign");
		PrivateKey privateKey = factory.generatePrivate(spec);
		Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        try {
		    cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        } catch (InvalidKeyException e) {
            RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
            RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
            Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
            cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, fakePublicKey);
        }

		byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
		String encryptedString = Base64.getEncoder().encodeToString(encryptedBytes);
		
		return encryptedString;
	}
	
	
	
	public static byte[][] genKeyPairBytes(int keySize)
			throws NoSuchAlgorithmException, NoSuchProviderException {
		byte[][] keyPairBytes = new byte[2][];

		KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
		gen.initialize(keySize, new SecureRandom());
		KeyPair pair = gen.generateKeyPair();

		keyPairBytes[0] = pair.getPrivate().getEncoded();
		keyPairBytes[1] = pair.getPublic().getEncoded();

		return keyPairBytes;
	}
	
	
	
	

}
